# Justfile for PostgreSQL deployment
# Usage: just <command> [args...]
# Variables can be set via environment variables or passed as arguments
#
# Note: This justfile only handles PostgreSQL deployment via Helm.

# -----------------------------
# Default variables (can be overridden via environment or command line)
# -----------------------------
DEFAULT_K8S_NAMESPACE := "netdisk"
DEFAULT_RELEASE_POSTGRES := "netdisk-postgresql"
DEFAULT_HELM_CHART_POSTGRES := "../../helm/postgres"
DEFAULT_KUBECONFIG_PATH := "~/.kube/config"

# -----------------------------
# Deploy Postgres: Deploy PostgreSQL using Helm
# -----------------------------
deploy K8S_NAMESPACE='' RELEASE_POSTGRES='' HELM_CHART_POSTGRES='' KUBECONFIG_PATH='':
    #!/usr/bin/env bash
    set -e
    
    # Priority: provided args (if not empty) > environment variables > defaults
    if [ -n "$K8S_NAMESPACE" ]; then
        FINAL_K8S_NAMESPACE="$K8S_NAMESPACE"
    else
        FINAL_K8S_NAMESPACE="${K8S_NAMESPACE:-{{DEFAULT_K8S_NAMESPACE}}}"
    fi
    
    if [ -n "$RELEASE_POSTGRES" ]; then
        FINAL_RELEASE_POSTGRES="$RELEASE_POSTGRES"
    else
        FINAL_RELEASE_POSTGRES="${RELEASE_POSTGRES:-{{DEFAULT_RELEASE_POSTGRES}}}"
    fi
    
    if [ -n "$HELM_CHART_POSTGRES" ]; then
        FINAL_HELM_CHART_POSTGRES="$HELM_CHART_POSTGRES"
    else
        FINAL_HELM_CHART_POSTGRES="${HELM_CHART_POSTGRES:-{{DEFAULT_HELM_CHART_POSTGRES}}}"
    fi
    
    if [ -n "$KUBECONFIG_PATH" ]; then
        FINAL_KUBECONFIG_PATH="$KUBECONFIG_PATH"
    else
        FINAL_KUBECONFIG_PATH="${KUBECONFIG_PATH:-{{DEFAULT_KUBECONFIG_PATH}}}"
    fi
    
    # Expand ~ in kubeconfig path
    FINAL_KUBECONFIG_PATH="${FINAL_KUBECONFIG_PATH/#\~/$HOME}"
    
    # Convert chart path to absolute if relative
    # Find project root (directory containing helm/ directory)
    if [[ ! "$FINAL_HELM_CHART_POSTGRES" = /* ]]; then
        PROJECT_ROOT="$(pwd)"
        while [ ! -d "${PROJECT_ROOT}/helm" ] && [ "${PROJECT_ROOT}" != "/" ]; do
            PROJECT_ROOT="$(dirname "${PROJECT_ROOT}")"
        done
        if [ -d "${PROJECT_ROOT}/helm" ]; then
            # Extract the chart name from the relative path
            # Examples: "../../helm/postgres" -> "helm/postgres", "helm/postgres" -> "helm/postgres"
            CHART_NAME=$(echo "${FINAL_HELM_CHART_POSTGRES}" | sed -E 's|^\.\./\.\./||' | sed -E 's|^\.\./||' | sed -E 's|^\./||')
            FINAL_HELM_CHART_POSTGRES="${PROJECT_ROOT}/${CHART_NAME}"
        else
            echo "Error: Could not find project root (directory containing helm/)"
            exit 1
        fi
    fi
    
    # Validate chart path exists
    if [ ! -d "${FINAL_HELM_CHART_POSTGRES}" ]; then
        echo "Error: Helm chart not found at ${FINAL_HELM_CHART_POSTGRES}"
        exit 1
    fi
    
    echo "Deploying Postgres..."
    echo "  Namespace: ${FINAL_K8S_NAMESPACE}"
    echo "  Release: ${FINAL_RELEASE_POSTGRES}"
    echo "  Chart: ${FINAL_HELM_CHART_POSTGRES}"
    echo "  Kubeconfig: ${FINAL_KUBECONFIG_PATH}"
    
    # Deploy using Helm (Helm will create namespace if needed)
    # Use --server-side auto and --force-conflicts for Helm 4.0 compatibility
    KUBECONFIG="${FINAL_KUBECONFIG_PATH}" helm upgrade --install "${FINAL_RELEASE_POSTGRES}" "${FINAL_HELM_CHART_POSTGRES}" \
        --namespace "${FINAL_K8S_NAMESPACE}" \
        --create-namespace \
        --server-side auto \
        --force-conflicts \
        --wait \
        --timeout 5m
    
    echo "Postgres deployment complete"

# -----------------------------
# Clean: Uninstall PostgreSQL from Kubernetes (optional)
# -----------------------------
clean K8S_NAMESPACE='' RELEASE_POSTGRES='' KUBECONFIG_PATH='' FORCE='false':
    #!/usr/bin/env bash
    set -e
    
    # Priority: provided args (if not empty) > environment variables > defaults
    if [ -n "$K8S_NAMESPACE" ]; then
        FINAL_K8S_NAMESPACE="$K8S_NAMESPACE"
    else
        FINAL_K8S_NAMESPACE="${K8S_NAMESPACE:-{{DEFAULT_K8S_NAMESPACE}}}"
    fi
    
    if [ -n "$RELEASE_POSTGRES" ]; then
        FINAL_RELEASE_POSTGRES="$RELEASE_POSTGRES"
    else
        FINAL_RELEASE_POSTGRES="${RELEASE_POSTGRES:-{{DEFAULT_RELEASE_POSTGRES}}}"
    fi
    
    if [ -n "$KUBECONFIG_PATH" ]; then
        FINAL_KUBECONFIG_PATH="$KUBECONFIG_PATH"
    else
        FINAL_KUBECONFIG_PATH="${KUBECONFIG_PATH:-{{DEFAULT_KUBECONFIG_PATH}}}"
    fi
    
    # Expand ~ in kubeconfig path
    FINAL_KUBECONFIG_PATH="${FINAL_KUBECONFIG_PATH/#\~/$HOME}"
    
    echo "Cleaning Postgres deployment..."
    echo "  Namespace: ${FINAL_K8S_NAMESPACE}"
    echo "  Release: ${FINAL_RELEASE_POSTGRES}"
    echo "  Kubeconfig: ${FINAL_KUBECONFIG_PATH}"
    
    # Check if release exists
    if KUBECONFIG="${FINAL_KUBECONFIG_PATH}" helm list --namespace "${FINAL_K8S_NAMESPACE}" | grep -q "${FINAL_RELEASE_POSTGRES}"; then
        echo "Uninstalling Helm release: ${FINAL_RELEASE_POSTGRES}"
        if [ "${FORCE}" = "true" ] || [ "${FORCE}" = "1" ]; then
            KUBECONFIG="${FINAL_KUBECONFIG_PATH}" helm uninstall "${FINAL_RELEASE_POSTGRES}" --namespace "${FINAL_K8S_NAMESPACE}" || true
        else
            KUBECONFIG="${FINAL_KUBECONFIG_PATH}" helm uninstall "${FINAL_RELEASE_POSTGRES}" --namespace "${FINAL_K8S_NAMESPACE}"
        fi
        echo "Postgres deployment removed"
    else
        echo "Helm release not found: ${FINAL_RELEASE_POSTGRES}"
    fi
    
    echo "Clean complete"
